package com.ruoyi.system.service.install.impl;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.shell.jsch.SshUtil;
import com.ruoyi.system.domain.install.DevServer;
import com.ruoyi.system.mapper.install.DevServerMapper;
import com.ruoyi.system.service.install.IDevServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务器Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-26
 */
@Slf4j
@Service
public class DevServerServiceImpl implements IDevServerService {
    @Resource
    private DevServerMapper devServerMapper;

    /**
     * 查询服务器
     *
     * @param id 服务器主键
     * @return 服务器
     */
    @Override
    public DevServer selectDevServerById(Long id) {
        return devServerMapper.selectDevServerById(id);
    }

    /**
     * 查询服务器列表
     *
     * @param devServer 服务器
     * @return 服务器
     */
    @Override
    public List<DevServer> selectDevServerList(DevServer devServer) {
        return devServerMapper.selectDevServerList(devServer);
    }

    /**
     * 新增服务器
     *
     * @param devServer 服务器
     * @return 结果
     */
    @Override
    public int insertDevServer(DevServer devServer) {
        devServer.setCreateTime(DateUtils.getNowDate());
        return devServerMapper.insertDevServer(devServer);
    }

    /**
     * 修改服务器
     *
     * @param devServer 服务器
     * @return 结果
     */
    @Override
    public int updateDevServer(DevServer devServer) {
        devServer.setUpdateTime(DateUtils.getNowDate());
        return devServerMapper.updateDevServer(devServer);
    }

    /**
     * 批量删除服务器
     *
     * @param ids 需要删除的服务器主键
     * @return 结果
     */
    @Override
    public int deleteDevServerByIds(Long[] ids) {
        return devServerMapper.deleteDevServerByIds(ids);
    }

    /**
     * 删除服务器信息
     *
     * @param id 服务器主键
     * @return 结果
     */
    @Override
    public int deleteDevServerById(Long id) {
        return devServerMapper.deleteDevServerById(id);
    }

    @Override
    public AjaxResult command(Long id, String command) {
        String a = null;
        DevServer devServer = devServerMapper.selectDevServerById(id);
        String host = devServer.getSshIp();
        int port = devServer.getSshPort();
        String user = devServer.getSshUser();
        String password = devServer.getSshPwd();
        try {
            //建立session
            Session session = SshUtil.getSession(host, port, user, password);
            //执行命令
            SshUtil.SshExecutionResult result = SshUtil.executeCommand(session, command);
            a = result.getOutput();
            // 处理结果...
        } catch (JSchException e) {
            log.error("异常内容：", e);
            return AjaxResult.error(e.getMessage());
        } finally {
            SshUtil.closeSession(host, port, user);
        }
        return AjaxResult.success(a);
    }
}
